package com.yiren.dbaa.module.device.view.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.yiren.backstage.communication.api.impl.SerialCommunicationApiImpl;
import com.yiren.backstage.communication.interfaces.DataCallback;
import com.yiren.backstage.communication.interfaces.OnRTKDataListener;
import com.yiren.backstage.communication.thread.RTKConnection;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.dbaa.R;
import com.yiren.dbaa.activity.BaseActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lwb
 * @date :2022/1/17 8:57
 * @description: 串口数据页面
 */
public class SerialPortDataActivity extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.et_serial_data)
    EditText etSerialData;    //串口数据读取
    @BindView(R.id.tv_action_data)
    TextView tvActionData;    //操作
    @BindView(R.id.tv_rtk_data)
    TextView tvRtkData;       //RTK数据读取

    private int flag = 0;  //0：串口数据 1：RTK数据
    private int type = 0;  //0：开始获取 1：结束获取

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_serialport_data);
        ButterKnife.bind(this);
        tvTitle.setText("串口数据");
        setData();
    }

    private void setData() {
        SerialCommunicationApiImpl.getInstance(this).setDataCallback(new DataCallback() {
            @Override
            public void onReadData(String data) {
                super.onReadData(data);
                if (flag == 0) setEditData(data);
            }
        });
        RTKConnection.getInstance(this).setOnRTKDataListener(new OnRTKDataListener() {
            @Override
            public void onRTKData(String data) {
                if (flag == 1) setEditData(data);
            }
        });
    }

    /**
     * 设置编辑框数据
     *
     * @param data
     */
    private void setEditData(String data) {
        if (type == 1) {
            runOnUiThread(() -> {
                if (!StringUtil.isEmpty(data)) {
                    etSerialData.append(data + "\n");
                }
                if (etSerialData.getText().toString().length() > 10000) {
                    etSerialData.setText("");
                }
            });
        }
    }

    @OnClick({R.id.iv_back, R.id.tv_action_data, R.id.tv_rtk_data})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:  //返回
                finish();
                break;
            case R.id.tv_action_data: //开始或停止获取
                if (flag == 1) {
                    flag = 0;
                    type = 0;
                    tvRtkData.setText("RTK获取");
//                    rtkConnection.setSaveStatus(false);
//                    rtkConnection.clearBasket();
                    etSerialData.setText("");
                }
                if (type == 0) {
                    type = 1;
                    tvActionData.setText("停止获取");
                } else if (type == 1) {
                    type = 0;
                    tvActionData.setText("开始获取");
                }
                break;
            case R.id.tv_rtk_data: //RTK读取
                if (flag == 0) {
                    flag = 1;
                    type = 0;
                    tvActionData.setText("开始获取");
                    etSerialData.setText("");
                }
                if (type == 0) {
                    type = 1;
                    tvRtkData.setText("RTK停止");
                } else if (type == 1) {
                    type = 0;
                    tvRtkData.setText("RTK获取");
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

}
